import { _decorator, Component, director, instantiate, Node, resources, UITransform } from 'cc';
import { display } from '../tools/display';
import { __XGameManager as XGameManager, xGameMgr } from '../XGameManager';
import { Audioer } from '../common/Audioer';
import { TYPE_LAYOUT_UI_AT } from '../../xg-types';
const { ccclass, property } = _decorator;

const ROOT_DIR = "/../x-game-fw/";

@ccclass('XGameFW')
export abstract class XGameFW extends Component {
    private static _self: XGameFW = null;
    /** XGameFW实例 */
    public static get inst() :XGameFW { return XGameFW._self };
    /** 是否加载《健康游戏忠告》 */
    protected get isLoadGameAdvice(): boolean { return true };
    /** 是否加载加载页面 */
    protected get isLoadLoading(): boolean { return true };

    public abstract firstStage(): string;

    protected onLoad(): void {
        
        XGameFW._self = this;
        // 初始化管理器
        XGameManager.init(this.node);
        // 初始化设备参数相关
        display.init(this.node);
        // 初始化音频
        Audioer.init(this.node);
        // 初始化UI层级
        this.initUILayouts();
        // 运行加载页
        this.loadStage();
    }

    protected abstract loaded(): void;

    private onXGameFWLoaded(type: "GA" | "LD"){
        if(type == "GA" && this.isLoadLoading) {
            this.playLoading();
        } else{
            this.loaded();
            this.playFirstStage();
        }
    }
    private loadStage(){
        if(this.isLoadGameAdvice){
            this.playGameAdvice();
        } else if(this.isLoadLoading) {
            this.playLoading();
        } else {
            this.loaded();
            this.playFirstStage();
        }
    }

    private playGameAdvice() {
        resources.load(ROOT_DIR + "prefabs/stages/GameAdvice", (err, data) => {
            console.log(err, data);
        });
        
    }
    /**加载页 */
    private playLoading(){
        // const node = instantiate(this.load_stage);
        // let ld = node.getComponent(Loading);
        // ld.remote_paths = this.getRemotePaths();
        // ld.firstStage = this.getFirstStage();
        // getUILayout(CL_LAYOUT_TYPE.UI).addChild(node);
    }

    private playFirstStage(){
        
    }

    public changeStage(stage: string){
        // director.loadScene(stage); 
    }


    /**
     * 首次进入游戏时, 创建出需要的UI节点
     */
    private initUILayouts() {
        XGameManager.addUILayout(TYPE_LAYOUT_UI_AT.BACKGROUND, "BACKGROUND");
        XGameManager.addUILayout(TYPE_LAYOUT_UI_AT.UI, "UI");
        XGameManager.addUILayout(TYPE_LAYOUT_UI_AT.POPUP, "POPUP");
        XGameManager.addUILayout(TYPE_LAYOUT_UI_AT.TOP, "TOP");
    }
    

    /** 设置需要远程加载的文件列表 */
    protected setRemoteFilepath(path: {[k: string]: string}){
        return null;
    };
    /**获取第一个页面 */
    // protected getFirstStage(): string;

    protected onEnable(): void {
        director.once("ON_XGAMEFW_LOADED", this.onXGameFWLoaded.bind(this));
    }

}


